Thumb

What is Sealed Class?

1/12/2020 1:06:13 AM

A sealed class is a class which is use the Sealed key word before write class. Sealed class have some specialty provide for programmer. Once we declare this classes are sealed then it can’t the inherit from other class. Even another sealed class can’t inherit from sealed class. We can notice the C# library class are use the sealed keyword so we can’t inherit the library class. String is also sealed class. Now given bellow the sealed class example code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testForClass1;

namespace testFor
{
   // Sealed class  
    public sealed class Human
    {
        public string Name { get; set; }
        private string Email { get; set; } 
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Human sealedClass = new Human();
            sealedClass.Name = "Farhan Sakib";
            Console.Read();
        }
    }
}

In this sealed class has two property Name and Email. Name field we can access by create the object. Email field is privet so we can’t access. Also we can’t inherit the Human class because this class is sealed class.